﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Mail_Box_Net_Disk.PublicTools
{
    class FileRW
    {
        /// <summary>
        /// 在应用云同步目录创建文件，如已有则替换
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="str">内容</param>
        /// <returns>写入是否成功</returns>
        public static async Task<bool> WriteFileRoamingFolder(string fileName, string str)
        {
            if (fileName == null)
            {
                return false;
            }

            StorageFolder folder;
            folder = ApplicationData.Current.RoamingFolder; //获取应用目录的文件夹
            try
            {
                var file_demonstration = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                //创建文件

                using (Stream file = await file_demonstration.OpenStreamForWriteAsync())
                {
                    using (StreamWriter write = new StreamWriter(file))
                    {
                        write.Write(str);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 在应用云同步目录创建文件，如已有则打开已有
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>读取到的字符</returns>
        public static async Task<string> ReadFileRoamingFolder(string fileName)
        {
            if (fileName == null)
            {
                return null;
            }
            StorageFolder folder;
            folder = ApplicationData.Current.RoamingFolder; //获取应用目录的文件夹

            try
            {
                var file_demonstration = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
                //创建文件
                string s;
                using (Stream file = await file_demonstration.OpenStreamForReadAsync())
                {
                    using (StreamReader read = new StreamReader(file))
                    {
                        s = read.ReadToEnd();
                    }
                }
                return s;
            }
            catch
            {
                return null;
            }
        }

        private static ApplicationDataContainer RoamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
        /// <summary>
        /// 获取应用云同步的设置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValue(string key) => RoamingSettings.Values[key];
        /// <summary>
        /// 修改应用云同步的设置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetValue(string key, object value) => RoamingSettings.Values[key] = value;
    }
}
